package pedongi.mobile.colorfinder.helper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import pedongi.mobile.colorfinder.FindActivity;

import android.R.attr;
import android.content.res.AssetManager;

public class InferenceEngine {
	private static InferenceEngine instance = null;
	private String[] attributes;
	private String[] nuances;
	private String[] nuancesFunction;
	public String ukuran = "";
	private ArrayList<Integer> answerSet = null;
	
	public String[] getAttributes() {
		return attributes;
	}

	private InferenceEngine() {
		attributes = new String[5];
		nuances = new String[6];	
		nuances[0] = "netral";
		nuances[1] = "netral";
		nuances[2] = "tenang";
		nuances[3] = "tenang";
		nuances[4] = "hangat";
		nuances[5] = "hangat";
		
		nuancesFunction = new String[8];
		nuancesFunction[0] = "tenang";
		nuancesFunction[1] = "tenang";
		nuancesFunction[2] = "hangat";
		nuancesFunction[3] = "hangat";
		nuancesFunction[4] = "netral";
		nuancesFunction[5] = "hangat";
		nuancesFunction[6] = "hangat";
		nuancesFunction[7] = "netral";
	}
	
	private String matchPattern(String line , ArrayList<String> attr) {
		String retval = "";
		String[] tokens = line.split(",");
		boolean match = true;
		
		if (attr.size() == tokens.length - 1) {
			for (int i = 0 ; i < attr.size() ; i++) {
				if (!tokens[i].equals("-")) {
					if (tokens[i].contains(";")) {
						String[] subtokens = tokens[i].split(";");
						boolean exists = false;
						for (int j = 0 ; j < subtokens.length ; j++) {
							if (subtokens[j].equals(attr.get(i)))
								exists = true;
						}
						if (!exists)
							match = false;
					}
					else {
						if (!attr.get(i).equalsIgnoreCase(tokens[i]))
							match = false;
					}
				}
			}
		}
		
		if (match)
			retval = tokens[tokens.length - 1];
		else
			retval = "UNMATCHED";
		
		return retval;
	}
	
	public String generateDebugText() {
		String retval = "";
		retval += attributes[0] + "\n";
		retval += attributes[1] + "\n";
		retval += attributes[2] + "\n";
		retval += ukuran + "\n";
		retval += attributes[3] + "\n";
		retval += attributes[4] + "\n";
		
		return retval;
	}
	
	public void generateAttributes(AssetManager assets , ArrayList<Integer> answerSet) {
		this.answerSet = answerSet;
		
		///Kelas warna
		if (answerSet.get(0) == 0)
			attributes[0] = "pastel";
		else if (answerSet.get(0) == 1)
			attributes[0] = "gelap";
		else if (answerSet.get(0) == 2)
			attributes[0] = "cerah";
		
		///Gaya
		if (answerSet.get(1) == 0)
			attributes[1] = "minimalis";
		else if (answerSet.get(1) == 1)
			attributes[1] = "modern";
		
		///Ruangan
		Integer panjang = answerSet.get(2);
		Integer lebar = answerSet.get(3);
		Integer tinggi = answerSet.get(4);
		
		if (lebar > 0 && panjang > 0 && tinggi > 0)
			attributes[2] = "besar";
		else
			attributes[2] = "kecil";
		
		///Generate ukuran ruangan
		if ((lebar == 0) || (lebar == -1))
		{
			if (panjang == 2)
			{
				ukuran = "koridor";
			}
			else
			{
				ukuran = "sempit";
			}
		}
		else if (panjang == 2)
		{
			ukuran = "luas";
		}
		else
		{
			ukuran = "sedang";
		}

		if (tinggi == 0)
		{
			if (ukuran != "sempit")
			{
				ukuran += " rendah";
			}
		}
		else if (tinggi == 2)
		{
			if ((ukuran != "luas") && (ukuran != "koridor"))
			{
				ukuran += " tinggi";
			}
		}
		
		///Nuansa
		if (answerSet.get(7) != -1)
			attributes[3] = nuances[answerSet.get(7)];
		else if (answerSet.get(5) != -1) {
			attributes[3] = nuancesFunction[answerSet.get(5)];
		}
		
		///Cahaya
		ArrayList<String> attr = new ArrayList<String>();
		attr.add(ukuran);
		attr.add(answerSet.get(8).toString());
		attr.add(answerSet.get(9).toString());
		attr.add(answerSet.get(10).toString());
		attr.add(answerSet.get(11).toString());
		if (answerSet.get(12) != -1)
			attr.add(answerSet.get(12).toString());
		else
			attr.add(answerSet.get(14).toString());
		attr.add(answerSet.get(13).toString());
		try {
			InputStream is = assets.open("csv/cahaya.csv");
			InputStreamReader isr = new InputStreamReader(is);
			
			BufferedReader reader = new BufferedReader(isr);
			
			//attributes[4] = reader.readLine();
			
			String line , result = "";
			attributes[4] = "";
			while ((line = reader.readLine()) != null) {
				result = matchPattern(line, attr);
				if (!result.equals("UNMATCHED"))
					attributes[4] = result;
			}
			
			if (attributes[4].equals(""))
				attributes[4] = "gelap";
			
			reader.close();
			isr.close();
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String getExplanation() {
		String retval = "";
		
		retval += "Karakter ruangan anda adalah ";
		for (int i = 0 ; i < attributes.length ; i++) {
			if (i == 0) retval += "warna ";
			retval += attributes[i];
			if (i < attributes.length - 1)
				retval += ", ";
			else
				retval += ".";
		}
		
		Interviewer interviwer = Interviewer.getInstance();
		
		retval += "\n\n";
		
		retval += "Ukuran ruangan anda adalah : ";
		for (int i = 2 ; i <= 4 ; i++) {
			if (answerSet.get(i) != -1) {
				retval += interviwer.getQuestionSet(i).getLabel() + " ";
				retval += interviwer.getQuestionSet(i).getOption().get(answerSet.get(i)) + ", ";
			}
		}
		retval += " dan pakar mengkategorikan ukuran ruang anda sebagai ruangan " + attributes[2] + ".\n\n";
		
		if (answerSet.get(7) != -1) {
			retval += "Nuansa yang ingin anda cipatakan adalah " + interviwer.getQuestionSet(7).getOption().get(answerSet.get(7));
		}
		else {
			retval += "Fungsi ruangan anda adalah " + interviwer.getQuestionSet(5).getOption().get(answerSet.get(5));
		}
		retval += " dan pakar mengkategorikan ruang anda memiliki kesan " + attributes[3] + ".\n\n";

		retval += "Keadaan sumber cahaya ruangan anda adalah : ";
		for (int i = 8 ; i <= 14 ; i++) {
			if (answerSet.get(i) != -1) {
				retval += interviwer.getQuestionSet(i).getLabel() + " ";
				retval += interviwer.getQuestionSet(i).getOption().get(answerSet.get(i)) + ", ";
			}
		}
		retval += " dan pakar mengkategorikan ukuran ruang anda dengan pencahayaan " + attributes[4] + ".";
		
		return retval;
	}
	
	public String getSetNumber() {
		return getSetNumber(attributes[0] , attributes[1] , attributes[2] , attributes[3] , attributes[4]);
	}

	public String getSetNumber(String kelasWarna, String gayaRuangan, String ruangan, String Nuansa, String cahaya) {
		String retval = "";
		int m_kelasWarna = -1, m_gayaRuangan = -1, m_Ruang = -1, m_Nuansa = -1, m_Cahaya = -1;
		
		if(kelasWarna.equalsIgnoreCase("pastel"))		m_kelasWarna = KELASWARNA_PASTEL;
		else if (kelasWarna.equalsIgnoreCase("gelap"))	m_kelasWarna = KELASWARNA_GELAP;
		else if (kelasWarna.equalsIgnoreCase("cerah"))	m_kelasWarna = KELASWARNA_CERAH;
		
		if(gayaRuangan.equalsIgnoreCase("minimalis"))	m_gayaRuangan = GAYARUANGAN_MINIMALIS;
		else if (gayaRuangan.equalsIgnoreCase("modern"))m_gayaRuangan = GAYARUANGAN_MODERN;
		
		if(ruangan.equalsIgnoreCase("besar"))		m_Ruang = RUANGAN_BESAR;
		else if (ruangan.equalsIgnoreCase("kecil"))	m_Ruang = RUANGAN_KECIL;
		
		if(Nuansa.equalsIgnoreCase("netral"))			m_Nuansa = NUANSA_NETRAL;
		else if (Nuansa.equalsIgnoreCase("tenang"))		m_Nuansa = NUANSA_TENANG;
		else if (Nuansa.equalsIgnoreCase("hangat"))		m_Nuansa = NUANSA_HANGAT;
		
		if(cahaya.equalsIgnoreCase("gelap"))			m_Cahaya = CAHAYA_GELAP;
		else if (cahaya.equalsIgnoreCase("terang"))		m_Cahaya = CAHAYA_TERANG;
		
		retval = String.valueOf(m_kelasWarna + m_gayaRuangan + m_Ruang + m_Nuansa + m_Cahaya + 1); 		
		return retval;
	}
	
	public static InferenceEngine getInstance() {
		if (instance == null)
			instance = new InferenceEngine();
		return instance;
	}
	
	//STATIC CONSTANT
	public static final int KELASWARNA_PASTEL		= 0;
	public static final int KELASWARNA_GELAP		= 24;
	public static final int KELASWARNA_CERAH		= 28;
	
	public static final int GAYARUANGAN_MINIMALIS	= 0;
	public static final int GAYARUANGAN_MODERN 		= 12;
	
	public static final int RUANGAN_KECIL 			= 0;
	public static final int RUANGAN_BESAR 			= 6;
	
	public static final int NUANSA_NETRAL 			= 0;
	public static final int NUANSA_TENANG 			= 2;
	public static final int NUANSA_HANGAT 			= 4;
	
	public static final int CAHAYA_GELAP 			= 0;
	public static final int CAHAYA_TERANG 			= 1;
}
